package ch.sv7.tool.dbmanager.db;

import ch.sv7.tool.dbmanager.db.difference.ConstraintDefinitionDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifferences;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class ConstraintSchema implements Comparable, Identifiable, SchemaElement {

	private TableSchema tableSchema;
	private String name;
	private String definition;
	
	/**
	 * @param tableSchema
	 * @param name
	 * @param definition
	 */
	public ConstraintSchema(TableSchema tableSchema, String name, String definition) {
		super();
		this.tableSchema = tableSchema;
		this.name = name;
		this.definition = definition;
	}

	public String getSchemaIdentifier() {
		return "Constraint: " + getTableSchema().getName() + "." + getName();
	}

	/**
	 * @return the tableSchema
	 */
	public TableSchema getTableSchema() {
		return tableSchema;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the definition
	 */
	public String getDefinition() {
		return definition;
	}

	public String getEntityType() {
		return "constraint";
	}

	public String getIdentity() {
		return name;
	}

	public void computeDifferencesFromActual(SchemaDifferences diffs, Object object) {
		if (!(object instanceof ConstraintSchema)){
			return;
		}
		ConstraintSchema actual = (ConstraintSchema)object;
		if (!actual.definition.equals(definition)){
			diffs.addDifference(this, new ConstraintDefinitionDifference(getTableSchema(), actual.getTableSchema(), actual, this));
		}
		
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((definition == null) ? 0 : definition.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ConstraintSchema other = (ConstraintSchema) obj;
		if (definition == null) {
			if (other.definition != null)
				return false;
		} else if (!definition.equals(other.definition))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}
